﻿using System;
using System.Collections.Generic;
using System.Text;

namespace 检测邮件服务器有效性
{
    public delegate void PrintForEachI(string Email, int CurrentIndex);
    public delegate void NotifyTimer();
    public class HandleForEach
    {
        int i_tick = 0 ;
        private string  _Emails;
        /// <summary>
        /// 多个邮件地址，可以是隔行，或者逗号，或者分号，分割开来的
        /// </summary>
        public string Emails
        {
            get { return _Emails; }
            set { _Emails = value; }
        }
        private char _splitCode = '\n';
        /// <summary>
        /// 分隔符，用来分割string，得到数组的
        /// </summary>
        public char SplitCode
        {
            get { return _splitCode; }
            set { _splitCode = value; }
        }
        /// <summary>
        /// 分割后的数组
        /// </summary>
        /// <returns></returns>
        public string[] ArrEmails()
        {
            return _Emails.Split(_splitCode); 
        }

        public void ForEachAll(PrintForEachI hde)
        {
            for (int i = 0; i < ArrEmails().Length;i++ )
            {
                if (!string.IsNullOrEmpty(ArrEmails()[i]))
                {
                    hde(ArrEmails()[i], i);
                }
               
            }
        }
        public void ForEachPart(int int_step, PrintForEachI hde,NotifyTimer nt)
        {
            int i_end = i_tick + int_step;
            if ((ArrEmails().Length - i_tick) < int_step) i_end = ArrEmails().Length;
            for (int i = i_tick; i < i_end; i++)
            {
                if (!string.IsNullOrEmpty(ArrEmails()[i]))
                {
                    hde(ArrEmails()[i], i);                  
                    if (i_tick == i_end - 1)
                    {
                        i_tick = i_end;
                        break;
                    }
                    i_tick++;
                }               
            }
            //如果循环完毕，则执行一个方法，进行处理
            if (i_end == ArrEmails().Length)
            {
                nt();
            } 
        }
    }
}
/* 用法1
 *      HandleForEach hfe = new HandleForEach();
 *      private void button3_Click(object sender, EventArgs e)
        {
            string FaXinDiZhi = textBox1.Text.Trim();
            string user = textBox2.Text.Trim();
            string pwd = textBox3.Text.Trim();
            string ems = richTextBox3.Text.Trim();
            richTextBox4.Text = "";

            hfe.Emails = ems;
            hfe.ForEach(test);
        }
        void test(string s,int idex)
        {
            richTextBox4.Text += getMailServer(s) + "\n";
            label7.Text = idex + 1 + "/" + hfe.ArrEmails().Length.ToString();
        }
 */